class ContractsController < ApplicationController
  before_action :set_contract, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @contracts = Contract.where(build_search(search_params)).order(created_at: :desc).page(params[:page])    
    respond_with(@contracts)
  end

  def show    
    render layout: "contract"
  end

  # By default, this action generates "SELL CONTRACT"
  def new
    @contract = Contract.new
    @contract.set_contract_code
    @contract.set_contract_date
    
    if params[:contract].present? && contract_params[:buyers].present?
      @buyer = Customer.find(contract_params[:buyers])   
      @contract.set_buyer_info(@buyer) 
    end
         
    @contract.set_contract_code
    @contract.set_contract_date
    @seller = Company.find(1)
    @contract.set_seller_info(@seller)
    
    10.times do |p|
      @contract.contract_items.build
    end
     
    respond_with(@contract)
  end


  # By default, this action generates "PURCHASE CONTRACT"
  def new_buy
    @contract = Contract.new   
    @contract.set_contract_code
    @contract.set_contract_date
    
    if ( params[:contract].present? && contract_params[:sellers].present? )
      @seller = Supplier.find(contract_params[:sellers]) 
      @contract.set_seller_info(@seller)     
    end
    
    # @sellers = Supplier.all
    @buyer = Company.find(1)
    
    10.times do |p|
      @contract.contract_items.build
    end
     
    respond_with(@contract)    
  end


  def edit
  end

  def create    
    @contract = Contract.new(contract_params)
    @contract.cal_total    
    @seller = @contract.get_seller_info
    @buyer = @contract.get_buyer_info
    
    respond_to do |format|
      if @contract.save        
        format.html { redirect_to contracts_path, notice: 'Contract was successfully created.' }
        format.json { render :show, status: :created, location: @contract }
      else
        if @contract.contract_type == "销售订单"
          format.html { render :new }
        else
          format.html { render :new_buy }  
        end        
        format.json { render json: @contract.errors, status: :unprocessable_entity }
      end
    end
    
    
  end

  def update
    @contract.cal_total
    @contract.update(contract_params)    
    
    respond_with(@contract)
  end

  def destroy
    @contract.destroy
    respond_with(@contract)
  end

  private
    def set_contract
      @contract = Contract.find(params[:id])
    end

    def contract_params
      params.require(:contract).permit(:sellers,:buyers,:contract_type,:contract_code, :contract_date, :place_of_fulfil, :total,:comment,
            :buyer, :buyer_address, :buyer_agent, :buyer_tel, :buyer_fax, :buyer_bank, :buyer_bank_account, :buyer_bank_address, 
            :seller, :seller_address, :seller_agent, :seller_tel, :seller_fax, :seller_bank, :seller_bank_account,:seller_bank_address,
            :quality, :packaging, :acceptance_term,:delivery_method,:delivery_date, :delivery_address, :payment_term,:jurisdiction, 
            contract_items_attributes: [:id,:product_name,:product_desc,:unit,:quantity,:unit_price,:amount,:comment]
            )
    end
    
    def search_params     
        if params[:contract].present?  
          contract_params.permit(:contract_code,:buyer,:seller)
        end                     
    end
    
    
end
